/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LOTTIETURBO_LOTTIE_RENDER_H
#define LOTTIETURBO_LOTTIE_RENDER_H

#include "color.h"
#include "render/lottie_render_layer.h"
#include "render/lottie_render_nativewindow.h"
#include "rlottiecommon.h"
#include <map>
#include <string>

namespace LottieTurbo {

class LottieRender {
public:
    LottieRender();
    ~LottieRender();

    /**
     *  渲染 LOTLayerNode
     * @param xcomponentId ID号
     * @param rootNode 当前要渲染的节点
     * @param calculatePosition 宽高属性
     * @param frame 当前帧号
     */
    void RenderTree(const std::string xcomponentId, const LOTLayerNode *rootNode, CalculatePosition *calculatePosition,
                    float frame);
    
    /**
     *  清除绘制面板
     * @param xcomponentId ID号
     */
    void GpuRenderClear(const std::string xcomponentId);

    /**
     *  渲染 LOTLayerNode 为图像数据
     * @param rootNode 解析后的帧数据
     * @param width 图片宽度
     * @param height 图片高度
     * @param bitmap 图片数据
     */
    bool RenderToImage(const LOTLayerNode *rootNode, float width, float height, void *bitmap);

    /**
     * 处理canvas
     * @param canvas
     * @param calculatePosition
     */
    void HandleCanvas(std::shared_ptr<drawing::Canvas> canvas, CalculatePosition *calculatePosition);

    /**
     * 使用帧缓存
     * @param rootNode 解析后的帧数据
     * @param width 宽
     * @param width 高
     * @param frame 帧号
     */
    bool RenderImageCache(const LOTLayerNode *rootNode, int32_t width, int32_t height, float frame);

    /**
     * 使用CPU渲染，适用于API12-15
     * @param xcomponentId ID号
     * @param rootNode 当前要渲染的节点
     * @param calculatePosition 宽高属性
     */
    void CpuRenderTree(const std::string xcomponentId, const LOTLayerNode *rootNode,
                       CalculatePosition *calculatePosition);
    /**
     *  清除绘制面板
     * @param xcomponentId ID号
     */
    void CpuRenderClear(const std::string xcomponentId);
    
    /**
     *  清除绘制面板
     * @param xcomponentId ID号
     */
    void RenderClear(const std::string xcomponentId);

    /**
     * @param xcomponentId ID号
     * @param rootNode 当前要渲染的节点
     * @param calculatePosition 宽高属性
     */
    void Render(const std::string xcomponentId, const LOTLayerNode *rootNode, CalculatePosition *calculatePosition,
                float frame);

    /**
     * 是否存在缓存
     * @param width 宽
     * @param width 高
     * @param frame 帧号
     */
    bool ImageCacheState(int32_t width, int32_t height, float frame);

private:
    struct ImageCache {
        int32_t width = 0;
        int32_t height = 0;
        drawing::Bitmap bitmap;
    };
    std::shared_ptr<LottieRenderLayer> lottieRenderLayer_{nullptr};
    std::shared_ptr<drawing::Canvas> canvas_{nullptr};
    OH_Drawing_GpuContext *gpuContext_{nullptr};
    OH_Drawing_Surface *surface_{nullptr};
    std::map<std::string, std::unique_ptr<ImageCache>> imageCache_;
    std::shared_ptr<LottieRenderNativeWindow> lottieRenderNativeWindow_ = nullptr;
};

} // namespace LottieTurbo

#endif // LOTTIETURBO_LOTTIE_RENDER_H